home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / OT PAPServerSample / PAPServerUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-28  |  3.6 KB  |  151 lines  |  [TEXT/CWIE]

  1. /*
  2.  
  3.     file: PAPServerUtilities.c
  4.     
  5.     by:        Rich Kubota
  6.             Developer Technical Support
  7.             
  8. */
  9.  
  10. #include "PAPServerUtilities.h"
  11.  
  12. /* the following code is from IM VI 3-8 which demonstrates how to check for the 
  13.  * availability of traps
  14.  */
  15.  
  16. short NumToolboxTraps(void)
  17. {
  18.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  19.         return(0x200);
  20.     else
  21.         return(0x400);
  22.  
  23. } /* NumToolboxTraps */
  24.  
  25. TrapType GetTrapType(short theTrap)
  26. {
  27.     short    trapMask = 0x0800;
  28.     
  29.     if (theTrap & trapMask)
  30.         return(ToolTrap);
  31.     else
  32.         return(OSTrap);
  33.  
  34. } /* GetTrapType */
  35.  
  36. Boolean TrapAvailable(short theTrap)
  37. {
  38.     TrapType    theType;
  39.     
  40.     theType = GetTrapType(theTrap);
  41.     if (theType == ToolTrap) {
  42.         theTrap &= 0x07FF;
  43.         if (theTrap >= NumToolboxTraps())
  44.             theTrap = _Unimplemented;
  45.     }
  46.     return(NGetTrapAddress(theTrap, theType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  47. }
  48.  
  49. /***************************************************************************
  50. The following function was taken from the MoreFiles.c DTS Sample code.
  51. **/
  52.  
  53. pascal    OSErr    HCreateMinimum(short vRefNum,
  54.                                long dirID,
  55.                                ConstStr255Param fileName)
  56. {
  57.     HParamBlockRec pb;
  58.  
  59.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  60.     pb.fileParam.ioVRefNum = vRefNum;
  61.     pb.ioParam.ioVersNum = 0;
  62.     pb.fileParam.ioDirID = dirID;
  63.     return ( PBHCreateSync(&pb) );
  64. }
  65.  
  66.  
  67. /* 
  68.  * OpenTempFile opens a temporary file on the root volume of the boot drive
  69.  * returns the file ref num to write to, or -1 if a problem occurred
  70.  *
  71.  * This code is not meant to demonstrate how one goes about creating a temporary
  72.  * file, it's more a matter of expediency for me.
  73.  */
  74. OSErr    OpenTempFile(short    *fRefNum)
  75. {
  76.     HParamBlockRec    pb;
  77.     short            index = 0;
  78.     OSErr            err;
  79.     
  80.     Str255            fname = "\pSavedPAPFile";
  81.                         //       123456789012        temp file name is 12 characters long
  82.     
  83.         // initialize err as dupFNErr since that and noErr are the only errors
  84.         // which we can deal with here
  85.     err = dupFNErr;
  86.     while (err == dupFNErr)
  87.     {
  88.         if (index < 10)
  89.         {
  90.             fname[0] = 14;    // specify that the file name is 14 character pascal string
  91.             fname[14] = '0' + index;
  92.         }
  93.         else if (index < 100)    // temp files 'SavedPAPFile 0' - 'SavedPAPFile 9' already exist
  94.         {
  95.             fname[0] = 15;
  96.             fname[14] = '0' + (index / 10);        // set the tens placeholder
  97.             fname[15] = '0' + (index % 10);        // set the units placeholder
  98.         }
  99.         else // this part of the code will likely not be tested, but it's implemented just in case.
  100.         {
  101.             fname[0] = 16;
  102.             fname[14] = '0' + (index / 100);        // set the hundreds placeholder
  103.             fname[15] = '0' + ((index % 100) / 10);    // set the tens placeholder
  104.             fname[16] = '0' + (index % 10);            // set the units placeholder
  105.         }
  106.         
  107.         index++;      // increment index for the next go around;
  108.         
  109.         err = HCreateMinimum(kBootVolVRefNum, kRootFolderDirID, fname);
  110.     }
  111.     
  112.         // was the file created successfully
  113.         
  114.     if (err == noErr)
  115.     {
  116.         pb.fileParam.ioNamePtr = (StringPtr) fname;
  117.         pb.ioParam.ioPermssn = fsWrPerm;
  118.         pb.ioParam.ioMisc = 0;
  119.         pb.fileParam.ioDirID = kRootFolderDirID;
  120.         pb.fileParam.ioVRefNum = kBootVolVRefNum;
  121.         err = PBHOpenSync(&pb);
  122.     }
  123.     
  124.     if (err != noErr)
  125.         *fRefNum = 0;
  126.     else
  127.         *fRefNum = pb.ioParam.ioRefNum;
  128.         
  129.     return err;
  130. }
  131.  
  132.  
  133. OSErr WriteDataToTempFile(short fRefNum, UInt8 *buffer, UInt32 len)
  134. {
  135.     HParamBlockRec    pb;
  136.  
  137.     pb.ioParam.ioRefNum = fRefNum;
  138.     pb.ioParam.ioBuffer = (Ptr)buffer;
  139.     pb.ioParam.ioReqCount = len;
  140.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;  /* fsAtMarK + noCacheBit */
  141.     pb.ioParam.ioPosOffset = 0;
  142.     return (PBWriteSync((ParmBlkPtr)&pb));    
  143. }
  144.  
  145. OSErr CloseTempFile(short fRefNum)
  146. {
  147.     HParamBlockRec    pb;
  148.  
  149.     pb.ioParam.ioRefNum = fRefNum;
  150.     return (PBCloseSync((ParmBlkPtr)&pb));    
  151. }